package com.zjkj.mp.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.channel.ChannelService;
import com.zjkj.mp.service.customer.GroupService;
import com.zjkj.mp.service.notice.NoticeManager;
import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.security.SpringSecurityUtils;
import com.zjkj.mp.vo.channel.MpChannel;
import com.zjkj.mp.vo.customer.Group;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 主界面跳转
 * 
 * @author 陈晨
 * @history <TABLE id="HistoryTable" border="1">
 *          <TR>
 *          <TD>时间</TD>
 *          <TD>描述</TD>
 *          <TD>作者</TD>
 *          </TR>
 *          <TR>
 *          <TD>2013-6-9</TD>
 *          <TD>创建初始版本</TD>
 *          <TD>陈晨</TD>
 *          </TR>
 *          </TABLE>
 */
@Controller
public class MainController {

	@Resource(name = "userServiceImpl")
	private UserService userService;
	@Resource(name = "channelServiceImpl")
	private ChannelService channelService;
	@Resource(name = "groupServiceImpl")
	private GroupService groupService;
	@Autowired
	NoticeManager noticeManager;

	/******************************* 页面跳转 ***********************************/

	/**
	 * 登录页
	 */
	@RequestMapping("/login.do")
	public void login(HttpServletRequest req) {
	}

	/**
	 * 首页
	 */
	@RequestMapping("/index.do")
	public String index(Model model, HttpServletRequest req,
			HttpServletResponse res) {

//		Cookie cookieuser = new Cookie("user", "admin@fingerkj.com" + "-" + "admin");
//		// 设定有效时间 以秒(s)为单位
//		cookieuser.setMaxAge(60);
//		// 设置Cookie路径和域名
//		cookieuser.setPath("/");
//		cookieuser.setDomain(".localhost"); // 域名要以“.”开头
//		res.addCookie(cookieuser);

		// 公告列表
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("visible", true);
		model.addAttribute("notices", this.noticeManager.getList(1, 10, params));

		model.addAttribute("username",
				((MpUser) SpringSecurityUtils.getCurrentUser()).getUsername());
		return "index";

	}

	/**
	 * 修改密码
	 */
	@RequestMapping("/changepwd.do")
	public void changepwd(HttpServletRequest request) {
	}

	@RequestMapping("/bbh.do")
	public void bbh(Model model, HttpServletRequest request) {
		// 读取版本信息
		try {
			String appServerHome = request.getSession().getServletContext()
					.getRealPath("/");
			File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");

			Manifest mf = new Manifest();
			mf.read(new FileInputStream(manifestFile));

			Attributes atts = mf.getMainAttributes();
			String versionStr = atts.getValue("Version-major") + "."
					+ atts.getValue("Version-minor") + "."
					+ atts.getValue("Version-incremental");
			model.addAttribute("version", versionStr);
			model.addAttribute("buildDate", atts.getValue("Build-date"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 跳转至发送页面
	 * 
	 * @return
	 */
	@RequestMapping("/sendMessage.do")
	public String sendMessage(HttpServletRequest req) {
		List<MpChannel> list = channelService.getMpChannelListStart(1);
		List<Group> listGroup = groupService.getGroupList(0);
		int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
		long messageCount = userService.findUserById(userId).getBalance();
		req.setAttribute("messageCount", messageCount);
		req.setAttribute("listGroup", listGroup);
		req.setAttribute("list", list);
		return "message/sendMessage";
	}

	/**
	 * 跳转至短信模板页面
	 * 
	 * @return
	 */
	@RequestMapping("/themeMessage.do")
	public String themeMessage() {
		return "message/themeMessage";
	}

	/**
	 * 跳转至发送报告页面
	 * 
	 * @return
	 */
	@RequestMapping("/sendInfo.do")
	public String sendInfo() {
		return "message/sendInfo";
	}

	/**
	 * 跳转至短信分组页面
	 * 
	 * @return
	 */
	@RequestMapping("/messageGroup.do")
	public String messageGroup() {
		return "message/messageGroup";
	}

	/**
	 * 跳转至财务明细页面
	 * 
	 * @return
	 */
	@RequestMapping("/financeMXInfo.do")
	public String financeMXInfo() {
		return "finance/financeMXInfo";
	}

	/**
	 * 跳转至联系人列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/customerList.do")
	public String customerList() {
		return "customer/customerList";
	}

	/**
	 * 跳转至通道列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/channelList.do")
	public String channelList() {
		return "channel/channelList";
	}

	/**
	 * 跳转至修改资料页面
	 * 
	 * @return
	 */
	@RequestMapping("/updateInfo.do")
	public String updateInfo() {
		return "account/updateInfo";
	}

}
